'''
Author: momochong0
Date: 2021-05-07 00:42:32
LastEditors: momochong0
LastEditTime: 2021-05-24 21:12:15
Description: 要推网络荣誉出品
'''
import re
import json
import requests
# 打印进度条的库
from tqdm import tqdm

# 如：https://v.douyin.com/e555vAG/
share = input('请输入复制的抖音分享链接：')
# 用正则解析链接
url = re.findall('https://v.douyin.com/.*?/', share)[0]
res = requests.get(url)
# vid解析，vid：视频id
vid = re.findall('/video/(.*?)/', res.url)[0]
# 调用api
api = f'https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={vid}'
res = requests.get(api).json()
# 视频下载链接解析
url = res['item_list'][0]['video']['play_addr']['url_list'][0]
# 去水印
url = url.replace('/playwm/', '/play/')
res = requests.get(url, headers={'user-agent': 'chrome'})
total_size = round(int(res.headers["Content-Length"]) / 1024 / 1024)
with open(f'{vid}.mp4', 'wb') as f:
    for chunk in tqdm(iterable=res.iter_content(1024 * 1024),
                      total=total_size,
                      unit='KB'):
        f.write(chunk)